/*
 * Copyright 2010 Interactive Media Management
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package dk.i2m.converge.jsf.beans;

import dk.i2m.converge.core.workflow.Outlet;
import dk.i2m.converge.core.security.SystemPrivilege;
import dk.i2m.converge.core.security.UserAccount;
import dk.i2m.converge.core.newswire.NewswireItem;
import dk.i2m.converge.core.newswire.NewswireService;
import dk.i2m.converge.ejb.facades.CalendarFacadeLocal;
import dk.i2m.converge.ejb.facades.NewsItemFacadeLocal;
import dk.i2m.converge.ejb.facades.OutletFacadeLocal;
import dk.i2m.converge.ejb.facades.UserFacadeLocal;
import dk.i2m.converge.ejb.facades.WorkflowFacadeLocal;
import dk.i2m.converge.ejb.services.NewswireServiceLocal;
import dk.i2m.converge.jsf.components.tags.DialogAssignment;
import dk.i2m.converge.jsf.components.tags.DialogEventSelection;
import dk.i2m.jsf.JsfUtils;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.event.ActionEvent;
import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;

/**
 * Application-scoped managed bean containing newswire articles.
 *
 * @author Allan Lykke Christensen
 */
public class Newswire {

    @EJB private NewswireServiceLocal newswireService;

    @EJB private OutletFacadeLocal outletFacade;

    @EJB private UserFacadeLocal userFacade;

    @EJB private CalendarFacadeLocal calendarFacade;

    @EJB private NewsItemFacadeLocal newsItemFacade;

    @EJB private WorkflowFacadeLocal workflowFacade;

    private NewswireService selectedService;

    private String search = "";

    private DataModel newsService = null;

    private DataModel news = null;

    private NewswireItem selectedItem = null;

    private DialogAssignment dialogAssignment;

    private DialogEventSelection dialogEventSelection;

    /**
     * Gets the {@link DataModel} containing the current news items of the newswire services.
     * 
     * @return {@link DataModel} containing the current news items of the newswire services.
     */
    public DataModel getNews() {
        if (news == null) {
            news = new ListDataModel(newswireService.getNews());
        }
        return news;
    }

    public DataModel getServices() {
        if (newsService == null) {
            newsService = new ListDataModel(getUserAccount().getNewswireServices());
        }

        return newsService;
    }

    public NewswireService getSelectedService() {
        return selectedService;
    }

    public void setSelectedService(NewswireService selectedService) {
        this.selectedService = selectedService;
    }

    public String getSearch() {
        return search;
    }

    public void setSearch(String search) {
        this.search = search;
    }

    public void onSearch(ActionEvent event) {
        List<NewswireItem> results = newswireService.search(getSearch());
        this.news = new ListDataModel(results);
    }

    public void onSelectService(ActionEvent event) {
        if (selectedService != null) {
            Long id = selectedService.getId();
            List<NewswireItem> serviceItems = newswireService.getNews(id);
            this.news = new ListDataModel(serviceItems);
        }
    }

    public void onShowAll(ActionEvent event) {
        news = new ListDataModel(newswireService.getNews());
    }

    private UserAccount getUserAccount() {
        final String valueExpression = "#{userSession.user}";
        return (UserAccount) JsfUtils.getValueOfValueExpression(valueExpression);
    }

    public NewswireItem getSelectedItem() {
        return selectedItem;
    }

    public void setSelectedItem(NewswireItem selectedItem) {
        this.selectedItem = selectedItem;
    }

    public void onAssign(ActionEvent event) {
        dialogAssignment = new DialogAssignment(outletFacade, workflowFacade, userFacade, newsItemFacade, calendarFacade, getOutlets());
        dialogAssignment.showStoryTab();
        dialogAssignment.getAssignment().setAssigned(true);
        dialogAssignment.getAssignment().setAssignedBy(getUserAccount());
        if (!getOutlets().isEmpty()) {
            dialogAssignment.getAssignment().setOutlet(getOutlets().iterator().next());
        }
        dialogAssignment.getAssignment().setTitle(selectedItem.getTitle());
        dialogAssignment.getAssignment().setBrief(selectedItem.getSummary());
        dialogAssignment.getAssignment().setStory(selectedItem.getContent());

        dialogAssignment.onChangeOutlet(null);

        dialogAssignment.getAssignment().setDeadline(java.util.Calendar.getInstance());
        dialogAssignment.getAssignment().getDeadline().setTimeZone(getUserAccount().getTimeZone());
        dialogAssignment.getAssignment().getDeadline().set(java.util.Calendar.HOUR_OF_DAY, 15);
        dialogAssignment.getAssignment().getDeadline().set(java.util.Calendar.MINUTE, 0);
        dialogAssignment.getAssignment().getDeadline().set(java.util.Calendar.SECOND, 0);

        dialogEventSelection = new DialogEventSelection(calendarFacade);
        dialogEventSelection.setAssignment(dialogAssignment.getAssignment());
    }

    /**
     * Gets a {@link List} of the {@link Outlet}s where the current user has
     * outlet planning privileges.
     *
     * @return {@link List} of the {@link Outlet}s where the current user has
     *         outlet planning privileges
     */
    public List<Outlet> getOutlets() {
        return getUserAccount().getPrivilegedOutlets(SystemPrivilege.OUTLET_PLANNING);
    }

    public DialogAssignment getDialogAssignment() {
        return dialogAssignment;
    }

    public void setDialogAssignment(DialogAssignment dialogAssignment) {
        this.dialogAssignment = dialogAssignment;
    }

    public DialogEventSelection getDialogEventSelection() {
        return dialogEventSelection;
    }

    public void setDialogEventSelection(DialogEventSelection dialogEventSelection) {
        this.dialogEventSelection = dialogEventSelection;
    }
}
